package com.jinxin.platform.cdsideparkmgr.service.impl;

import com.jinxin.platform.base.common.pojo.JsonResult;
import com.jinxin.platform.cdsideparkmgr.bean.CdyardmgrChargeConfig;
import com.jinxin.platform.cdsideparkmgr.mapper.CdsideparkChargeConfigMapper;
import com.jinxin.platform.cdsideparkmgr.service.ICdsideparkmgrChargeConfigService;
import com.jinxin.platform.cdsideparkmgr.utils.IdWorker;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;

/**
 * All rights Reserved, Designed By www.beonelot.com
 *
 * @version: V1.0
 * @Copyright: www.beonelot.com Inc. All rights reserved.
 * @Description：
 * @Date: 2021-09-06
 * @Author: Lingnan
 **/
@Service
@Slf4j
public class CdsideparkmgrChargeConfigServiceImpl implements ICdsideparkmgrChargeConfigService {
    @Autowired
    CdsideparkChargeConfigMapper cdsideparkChargeConfigMapper;
    @Resource
    private IdWorker idWorker;


    @Override
    public CdyardmgrChargeConfig selectOne(HashMap param) {
        return cdsideparkChargeConfigMapper.selectOne(param);
    }

    @Override
    public List<CdyardmgrChargeConfig> list(HashMap param) {
        return cdsideparkChargeConfigMapper.list(param);
    }

    @Override
    public List<CdyardmgrChargeConfig> listPage(HashMap param) {
        return cdsideparkChargeConfigMapper.listPage(param);
    }

    @Override
    public int listPageCount(HashMap param) {
        return cdsideparkChargeConfigMapper.listPageCount(param);
    }

    @Override
    public JsonResult delete(String id) {
        if(StringUtils.isEmpty(id)){
            return JsonResult.error("Id不存在");
        }
        int result = cdsideparkChargeConfigMapper.deleteByKey(id);
        if(result > 0){
            return JsonResult.ok("删除成功","");
        }
        return JsonResult.error("删除失败");
    }

    @Override
    public JsonResult saveOrUpdate(CdyardmgrChargeConfig record) {
        if (record == null){
            return JsonResult.error("参数错误，操作失败");
        }
        String id = record.getId();
        int count;
        String msg = "";
        if(StringUtils.isEmpty(id)){
            HashMap param = new HashMap();
            param.put("yardId",record.getYardId());
            CdyardmgrChargeConfig chargeConfig = cdsideparkChargeConfigMapper.selectOne(param);
            if(chargeConfig == null){
                record.setId(String.valueOf(idWorker.nextId()));
                count = cdsideparkChargeConfigMapper.insert(record);
                msg = "新增成功";
            }else {
                chargeConfig.setRuleId(record.getRuleId());
                chargeConfig.setRule(record.getRule());
                count = cdsideparkChargeConfigMapper.updateByKey(chargeConfig);
                msg = "更新成功";
            }
        }else {
            count = cdsideparkChargeConfigMapper.updateByKey(record);
            msg = "更新成功";
        }
        if(count > 0){
            return JsonResult.ok(msg,"");
        }else {
            return JsonResult.error("操作失败，请重试");
        }
    }

}
